package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.OperationDiagnosisConfig;
import com.zp.base.entity.OperationDict;
import com.zp.base.entity.OperationDictIcd10;
import com.zp.base.entity.bo.OperationDiagnosisConfigBo;
import com.zp.base.entity.vo.OperationDiagnosisConfigVo;
import com.zp.base.mapper.OperationDiagnosisConfigMapper;
import com.zp.base.mapper.OperationDictIcd10Mapper;
import com.zp.base.mapper.OperationDictMapper;
import com.zp.base.service.IOperationDiagnosisConfigService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 诊断类型、手术字典类型配置Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-11
 */
@RequiredArgsConstructor
@Service
public class OperationDiagnosisConfigServiceImpl implements IOperationDiagnosisConfigService {

    private final OperationDiagnosisConfigMapper baseMapper;

    private final OperationDictMapper operationDictMapper;

    private final OperationDictIcd10Mapper icd10Mapper;

    /**
     * 查询诊断类型、手术字典类型配置
     */
    @Override
    public OperationDiagnosisConfigVo queryById (String orgId) {
        return baseMapper.selectVoById(orgId);
    }

    /**
     * 查询诊断类型、手术字典类型配置列表(分页)
     */
    @Override
    public TableDataInfo<OperationDiagnosisConfigVo> queryPageList (OperationDiagnosisConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OperationDiagnosisConfig> lqw = buildQueryWrapper(bo);
        Page<OperationDiagnosisConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询诊断类型、手术字典类型配置列表(不分页)
     */
    @Override
    public List<OperationDiagnosisConfigVo> queryList (OperationDiagnosisConfigBo bo) {
        LambdaQueryWrapper<OperationDiagnosisConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OperationDiagnosisConfig> buildQueryWrapper (OperationDiagnosisConfigBo bo) {
        LambdaQueryWrapper<OperationDiagnosisConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(OperationDiagnosisConfig::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosisType()), OperationDiagnosisConfig::getDiagnosisType, bo.getDiagnosisType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), OperationDiagnosisConfig::getOperationType, bo.getOperationType());
        return lqw;
    }

    /**
     * 新增诊断类型、手术字典类型配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo (OperationDiagnosisConfigBo bo) {
        OperationDiagnosisConfig add = BeanUtil.toBean(bo, OperationDiagnosisConfig.class);
        // 删除机构存在的诊断、手术配置
        baseMapper.delete(Wrappers.<OperationDiagnosisConfig>lambdaQuery()
                .eq(OperationDiagnosisConfig::getOrgId, bo.getOrgId()));
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrgId(add.getOrgId());
            if (StringUtils.isNotBlank(bo.getOperationType())) {
                saveOperationDict(bo);
            }
        }
        return flag;
    }

    public void saveOperationDict (OperationDiagnosisConfigBo bo) {
        List<OperationDict> operationDictList = new ArrayList<>();
        List<OperationDictIcd10> icd10List = icd10Mapper.selectList(Wrappers.<OperationDictIcd10>lambdaQuery()
                .eq(OperationDictIcd10::getDiagnosisOperType, bo.getOperationType()));
        if (CollectionUtil.isNotEmpty(icd10List)) {
            icd10List.forEach(item -> {
                item.setOperationDictId(null);
                OperationDict operationDict = BeanUtil.toBean(item, OperationDict.class);
                operationDict.setOrgId(bo.getOrgId());
                operationDict.setDelFlag("0");
                operationDictList.add(operationDict);
            });
            operationDictMapper.insertBatch(operationDictList);
        } else {
            throw new RuntimeException("暂未维护手术字典类型为" + bo.getOperationType() + "的数据，请维护后再操作");
        }
    }

    /**
     * 修改诊断类型、手术字典类型配置
     */
    @Override
    public Boolean updateByBo (OperationDiagnosisConfigBo bo) {
        OperationDiagnosisConfig update = BeanUtil.toBean(bo, OperationDiagnosisConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (OperationDiagnosisConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除诊断类型、手术字典类型配置
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
